package com.itheima.stock.mq;


import com.github.benmanes.caffeine.cache.Cache;
import com.itheima.stock.servier.StockService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 定义股票相关mq消息监听
 */
@Component
@Slf4j
public class StockMqMsgListener {

@Autowired
private Cache<String,Object> caffeineCache;

@Autowired
private StockService stockService;
    @RabbitListener(queues = "innerMarketQueue")
    public void refreshInnerMarketInfo(Date startTime){
        //统计当前时间点与发送消息时时间点的差值，如果超过1分钟，则告警
        // DateTime.now().getMillis() 当前时间点的毫秒数
       long diffTime = DateTime.now().getMillis() - new DateTime(startTime).getMillis();
        if (diffTime>60000) {
            log.error("大盘发送消息时间{},延时毫秒数{}",new DateTime(startTime).toString("yyyy-MM-dd HH:mm:ss"),diffTime);
        }
        //刷新缓存
        //剔除旧数据
        caffeineCache.invalidate("innerMarketKey");
        //调用服务方法，刷新数据  -->旧数据已经删了，已经拿不到数据了，getInnerMarket()的回调就是拿不到数据就去数据加载。。。
        stockService.getInnerMarket();
    }
}

